package com.atguigu.guli.service.edu.controller.admin;


import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.edu.entity.Chapter;
import com.atguigu.guli.service.edu.service.ChapterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2021-07-27
 */
@RestController

@RequestMapping("/admin/edu/chapter")
public class AdminChapterController {
    @Autowired
    ChapterService chapterService;

    //1、新增
    @PostMapping("save")
    public R saveChapter(@RequestBody Chapter chapter){
        chapterService.save(chapter);
        return R.ok();
    }

    //2、根据id查询
    @GetMapping("getById/{id}")
    public R getById(@PathVariable String id){
        Chapter chapter = chapterService.getById(id);
        return R.ok().data("item",chapter);
    }

    //3、更新
    @PutMapping("update")
    public R updateChapter(@RequestBody Chapter chapter){
        chapterService.updateById(chapter);
        return R.ok();
    }


    //4、删除
    @DeleteMapping("remove/{id}")
    public R removeById(@PathVariable String id){
        chapterService.removeById(id);
        return R.ok();
    }



    //5、查询:查询章节嵌套集合
    @GetMapping("getNestedChapters/{courseId}")
    public R getNestedChapters(@PathVariable String courseId){
        List<Chapter> chapters = chapterService.getNestedChapters(courseId);
        return R.ok().data("items",chapters);
    }
}

